//
//  CSYVistorView.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/8.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

@objc protocol CSYVisitorViewDelegate : NSObjectProtocol{

    //点击了登陆按钮
    optional func startLogin()
    
    //点击了注册按钮
    optional func startRegist()

}

class CSYVisitorView: UIView {

    //设置代理属性
    weak var delegate : CSYVisitorViewDelegate?
    
    //实现代理方法
    @objc func clickLoginButton (){
    
        delegate?.startLogin?()
    
    }
    
    @objc func clickRegistButton (){
    
        delegate?.startRegist?()
    
    }
    
    //设置载入界面
    func setViewItems( iconName : String? , tipsInfo : String ){
    
        tipsLabel.text = tipsInfo
        
        insideView.hidden = false
        
        if(iconName != nil){
        
            outCircleView.image = UIImage(named:iconName!)
            
            bringSubviewToFront(outCircleView)
            
            insideView.hidden = true
        
        }else{
            
            startAnimation()
        
        }
    
    }
    
    //添加动画效果
    private func startAnimation () {
    
        let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        outCircleView.layer.addAnimation(animation, forKey: "outCircleViewAnimation")
    
        insideView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //访客视图的编辑
    
    func setUpUI () {
    
        //添加主要图片
        addSubview(outCircleView)
        
        //添加遮罩
        addSubview(coverView)
        
        //添加内容图片
        addSubview(insideView)
        
        //添加文字说明
        addSubview(tipsLabel)
        
        //添加登陆按钮
        addSubview(loginButton)
        
        //添加注册按钮
        addSubview(registerButton)
        
        //设置Frame约束失效
        for view in subviews {
        
            view.translatesAutoresizingMaskIntoConstraints = false
        
        }
        
        //设置约束
        
        //设置主要图片的约束
        //设置X轴约束
        /**
        约束设置
        
        - parameter NSLayoutConstraintitem: 约束对象
        - parameter attribute:              约束参数:约束对象的各个位置点[PowerPoint]
        - parameter relatedBy:              约束范围:[大于||等于||小于]
        - parameter toItem:                 参照对象
        - parameter attribute:              参照对象参数:参照对象的各个位置点[PowerPoint]
        - parameter multiplier:             约束参数(一般为1.0)
        - parameter constant:               约束范围(即相对位置)
        
        - returns: 约束值
        */
        addConstraint(NSLayoutConstraint(item: outCircleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        //设置Y轴约束
        addConstraint(NSLayoutConstraint(item: outCircleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60.0))
    
        //设置内容图片的主要约束
        //设置X约束
        addConstraint(NSLayoutConstraint(item: insideView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: outCircleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        //设置Y约束
        addConstraint(NSLayoutConstraint(item: insideView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: outCircleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
        
        //设置提示文字的主要约束
        //设置X约束
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: outCircleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
    
        //设置Y约束
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: outCircleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 5))
        
        //设置宽约束
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        //设置高约束
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 45))
        
        //设置登陆按钮的约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0.0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置注册按钮的主要约束
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0.0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
    
        //设置遮罩View的主要约束
        
        //横向约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[coverView]-0-|", options: [], metrics: nil, views: ["coverView" : coverView]))
        
        //纵向约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[coverView]-(-35)-[loginButton]", options: [], metrics: nil, views: ["coverView" : coverView , "loginButton" : loginButton]))
        
//        //点击了登陆按钮
//        func clickLoginButton()
//        
//        //点击了注册按钮
//        func clickRegistButton()

        
        self.loginButton.addTarget(self, action: "clickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.registerButton.addTarget(self, action: "clickRegistButton", forControlEvents: UIControlEvents.TouchUpInside)
        
    
    }
    
    
    //重写初始化方法
    init () {
    
        super.init(frame: CGRectZero)
    
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载所有的控件
    
    lazy var outCircleView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var insideView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var coverView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var tipsLabel : UILabel = {
    
        let label:UILabel = UILabel ()
    
        label.text = "关注一些人，回这里看看有什么惊喜" ;
        
        label.textColor = UIColor.lightGrayColor()
    
        label.textAlignment = NSTextAlignment.Center
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.numberOfLines = 0
        
        return label
        
    }()
    
    //懒加载登陆按钮
    lazy var loginButton : UIButton = {
    
        let loginBtn : UIButton = UIButton()
        
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        
        loginBtn.setBackgroundImage(UIImage (named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginBtn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        
        return loginBtn
    
    }()
    
    //懒加载注册按钮
    lazy var registerButton : UIButton = {
    
        let registerBtn : UIButton = UIButton ()
        
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        
        registerBtn.setBackgroundImage(UIImage (named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        return registerBtn
    
    }()
    
    
    
}
